//
//  ThemeButton.m
//  CloudWeibo
//
//  Created by CLOUD on 11/20/14.
//  Copyright (c) 2014 Cloud. All rights reserved.
//

#import "ThemeButton.h"
#import "ThemeManager.h"

@implementation ThemeButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithImage:(NSString *)imageName highlightedImage:(NSString *)highlightedImage
{
    self = [self init];
    if (self) {
        self.imageName = imageName;
        self.highligthImageName = highlightedImage;
    }
    return self;
}

- (id)initWithBackgroundImage:(NSString *)imageName highlightedImage:(NSString *)highlightedImage
{
    self = [self init];
    if (self) {
        self.backImageName = imageName;
        self.backHighligthImageName = highlightedImage;
    }
    return self;
}

- (id) init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeNotification) name:kThemeDidChangeNofication object:nil];
    }
    return self;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)themeNotification {
    [self loadImage];
}

- (void)setLeftCapWidth:(int)leftCapWidth
{
    _leftCapWidth = leftCapWidth;
    [self loadImage];
}

- (void)setTopCapHeight:(int)topCapHeight
{
    _topCapHeight = topCapHeight;
    [self loadImage];
}

- (void)loadImage {
    ThemeManager *themeManger = [ThemeManager shareInstance];
    
    UIImage *image = [themeManger getThemeImage:self.imageName];
    image = [image stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight];
    UIImage *highlightImage = [themeManger getThemeImage:self.highligthImageName];
    highlightImage = [highlightImage stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight];
    
    [self setImage:image forState:UIControlStateNormal];
    [self setImage:highlightImage forState:UIControlStateHighlighted];
    
    UIImage *backImage = [themeManger getThemeImage:self.backImageName];
    backImage = [backImage stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight];
    UIImage *backHighlightImage = [themeManger getThemeImage:self.backHighligthImageName];
    backHighlightImage = [backHighlightImage stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight];
    [self setBackgroundImage:backImage forState:UIControlStateNormal];
    [self setBackgroundImage:backHighlightImage forState:UIControlStateHighlighted];
}

- (void)setImageName:(NSString *)imageName{
    if (_imageName != imageName) {
        [_imageName release];
        _imageName = [imageName copy];
    }
    [self loadImage];
}

- (void)setHighligthImageName:(NSString *)highligthImageName{
    if (_highligthImageName != highligthImageName) {
        [_highligthImageName release];
        _highligthImageName = [highligthImageName copy];
    }
    [self loadImage];
}

@end
